package com.carlinfo.back.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.servlet.BaseServlet;
import com.carlinfo.userscenter.stu.pojo.Answer;
import com.carlinfo.userscenter.stu.service.IQuestionService;
import com.carlinfo.userscenter.stu.service.IStudentService;
import com.carlinfo.userscenter.stu.service.impl.QuestionServiceImpl;
import com.carlinfo.userscenter.stu.service.impl.StudentServiceImpl;

public class AnswerBackServlet extends BaseServlet
{
	IStudentService studentService = new StudentServiceImpl();
	IQuestionService questionService = new QuestionServiceImpl();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		request.setCharacterEncoding("UTF-8");
		this.logger.info("===========DemoBackServlet===========");
		String param = request.getParameter("method");
		if ("answerUpdate".equalsIgnoreCase(param))
		{
			this.answerUpdate(request, response);
		}else if ("answerUpdateSubmit".equalsIgnoreCase(param))
		{
			this.answerUpdateSubmit(request, response);
		}
	}
	private void answerUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		int id = Integer.valueOf(request.getParameter("id"));
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		Answer answer = questionService.selectOneAnswerService(condMap);
		String jieGuo = request.getParameter("jieGuo");
		byte status = Byte.valueOf(request.getParameter("status"));
		boolean shiFou = "1".equals(request.getParameter("shiFou")) ? true : false;
		answer.setPubTime(new Date());
		answer.setEndTime(new Date());
		answer.setJieGuo(jieGuo);
		answer.setShiFou(shiFou);
		answer.setStatus(status);
		JSONObject jsonObject = this.questionService.updateOneAnswerService(answer);
		request.setAttribute("json", jsonObject);
		String path = "/WEB-INF/jsp/back/answer/answerInsert.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void answerUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		int id = Integer.valueOf(request.getParameter("id"));
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		Answer answer = questionService.selectOneAnswerService(condMap);
		request.setAttribute("answer", answer);
		String path = "/WEB-INF/jsp/back/answer/answerInsert.jsp";
		request.getRequestDispatcher(path).forward(request, response);
		
//		String path = "/WEB-INF/jsp/back/admins/adminsUpdate.jsp";
//		request.getRequestDispatcher(path).forward(request, response);
	}
}
